<?php


namespace Frieza\Modules\BasicCheck\Actions;

use Doraemon\cache\User as UserCache;
use Doraemon\service\BasicCheck\CheckAvatarInfo;
use Doraemon\service\BasicCheck\CheckCommonInfo;
use Doraemon\service\Member as sMember;
use Doraemon\tools\Log;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class SetDefaultAvatar extends RowAction
{
    public $name = '置为默认头像';
    const TOAST_ALERT = '你确定要置为默认头像吗？';

    public function handle($id,Request $request)
    {

        Log::error("reasonsViolation",[$request->input("reasonsViolation")]);

        $adminId = Admin::user()->id;
        //$command->reasonsViolation = $request->input("reasonsViolation");

        $reasonsViolation =  $request->input("reasons_violation",'');
        $script =  $request->input("script",'');
        $otherReason =  $request->input("other_reason",'');

        $isErr = empty($otherReason) && $reasonsViolation == CheckCommonInfo::VIOLATION_TYPE_OTHER
            || !empty($otherReason) && $reasonsViolation != CheckCommonInfo::VIOLATION_TYPE_OTHER;

        if($isErr){
            return $this->response()->error('违规原因必须选择其他,其他原因才能填写内容！');
        }
        $data = [
            'admin_id' => $adminId,
            'check_status' => CheckCommonInfo::CHECK_STATUS_NO,
            'check_time' => time(),
            'reasons_violation' => $reasonsViolation,
            'other_reason' => $otherReason,
            'script' => $script,
        ];
        $invalidData = [
            'admin_id' => $adminId,
            'check_status' => CheckCommonInfo::CHECK_STATUS_INVALID,
            'check_time' => time(),
        ];
        $info = CheckAvatarInfo::getInfo(['id' => $id]);
        if (!empty($info)) {
            $res = CheckAvatarInfo::updateInfo(['id' => $id],$data);
            //将uid下别的数据改为无效
            CheckAvatarInfo::updateInfo(['uid' => $info['uid'], 'check_status' => 0, 'id[!]' => $id ],$invalidData);


            sMember::setUserDefaultAvatar($info['uid']);
            UserCache::deleteUserInfo($info['uid']);
            //发送下行消息
            if (CheckCommonInfo::VIOLATION_TYPE_OTHER == $reasonsViolation) {
                $msg = $script;
            } else {
                $msg = CheckCommonInfo::SCENES_AVATAR[$reasonsViolation];
            }
            CheckCommonInfo::sendMsg($info['uid'], $msg);
            if ($res) {
                return $this->response()->success('设置成功')->refresh();
            } else {
                return $this->response()->error('操作失败,稍后重试!');
            }
        } else {
            return $this->response()->error('暂无数据，不需要操作!');
        }




    }

    public function retrieveModel(Request $request)
    {

        Log::error("retrieveModel,reasonsViolation",[$request->input("reasonsViolation")]);

        if (!$key = $request->get('_key')) {
            return false;
        }

        Log::error("reasonsViolation",[$request->input("reasonsViolation")]);

        return $key;
    }


    public function form()
    {

        $this->select('reasons_violation', '违规原因')->options(CheckCommonInfo::VIOLATION_REASON);
        $this->text("other_reason","其他违规原因");
        $this->textarea("script","话术");

    }


}